from django.db import models
from apitest.utils.Validate import validate_unique
# Create your models here.
class Project(models.Model):
    projectName = models.CharField(verbose_name='项目名称', max_length=255,validators=[validate_unique('Project',"projectName")])
    descripTion = models.TextField(verbose_name='项目介绍',blank=True)

    def __str__(self):
        return self.projectName
    # def get_case_count(self):
    #     return self.test_cases.count()
    class Meta:
        verbose_name = '项目'
        verbose_name_plural = '项目'

#脚本信息相关字段
class ScriptStep(models.Model):
    scriptName = models.CharField(verbose_name='脚本名称', max_length=255)
    projectName = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='script_cases', blank= True)
    index = models.PositiveIntegerField(verbose_name='步骤序号')
    url = models.CharField(verbose_name='URL', max_length=255)
    method = models.CharField(verbose_name='请求方法', max_length=255)
    headers = models.JSONField(verbose_name='请求头')
    params = models.JSONField(verbose_name='查询参数',blank=True,null=True)
    payload = models.JSONField(verbose_name='请求体')
    data = models.JSONField(verbose_name='响应数据')
    res = models.JSONField(verbose_name='响应结果')
    isdelete = models.BooleanField(verbose_name="是否被删除", default=False)
    times = models.IntegerField(verbose_name="调用次数",default=0)

    def __str__(self):
        return f'Step {self.index} - {self.url}'

    class Meta:
        verbose_name = '脚本步骤'
        verbose_name_plural = '脚本步骤'
        ordering = ['index']

#请求列表信息相关字段
class RequestsInfo(models.Model):
    title= models.CharField(verbose_name='名称', max_length=255)
    index = models.PositiveIntegerField(verbose_name='步骤序号')
    method = models.CharField(verbose_name='请求方式', max_length=255)
    url = models.CharField(verbose_name='URL', max_length=255)
    headers = models.JSONField(verbose_name='请求头')
    params = models.JSONField(verbose_name='查询参数',blank=True,null=True)
    payload = models.JSONField(verbose_name='请求体')
    data = models.JSONField(verbose_name='表格数据')
    res = models.JSONField(verbose_name='响应结果')
    isdelete = models.BooleanField(verbose_name="是否被删除",default=False)
    # test_case = models.ForeignKey(TestCase, on_delete=models.CASCADE, related_name='script_steps')

    def __str__(self):
        return f'Step {self.index} - {self.url}'

    class Meta:
        verbose_name = '请求列表'
        verbose_name_plural = '请求列表'
        # ordering = ['step_number']
